package com.sch.wanandroid.ui.my.collect.addCollect

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.base.BaseActivity
import com.sch.wanandroid.databinding.ActivityAddArticleBinding
import com.sch.wanandroid.databinding.ActivityAddCollectBinding

/**
 * Created by Sch.
 * Date: 2020/12/23
 * description:用于收藏站外文章
 */
class AddCollectActivity : BaseActivity() {
    private lateinit var addCollectViewModel: AddCollectViewModel
    private lateinit var databinding: ActivityAddCollectBinding

    override fun getLayoutId(): Int {
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_add_collect)
        databinding.lifecycleOwner = this
        return 0
    }

    override fun init(savedInstanceState: Bundle?) {
        initTitle("添加收藏")
        addCollectViewModel = ViewModelProvider(this).get(AddCollectViewModel::class.java)
        databinding.data = addCollectViewModel
        databinding.llCollect.setOnClickListener {
            if (addCollectViewModel.titleString.value.isNullOrEmpty()) {
                SmartToast.fail("标题不能为空")
                return@setOnClickListener
            }
            if (addCollectViewModel.authorString.value.isNullOrEmpty()) {
                SmartToast.fail("作者不能为空")
                return@setOnClickListener
            }
            if (addCollectViewModel.linkString.value.isNullOrEmpty()) {
                SmartToast.fail("链接不能为空")
                return@setOnClickListener
            }
            setViewStatus(false)
            addCollectViewModel.addCollect()
        }
        addCollectViewModel.addSuccess.observe(this) {
            if (it) {
                SmartToast.success("收藏成功")
                finish()
            }
        }
        addCollectViewModel.errorMsg.observe(this) {
            setViewStatus(true)
            SmartToast.fail(it)
        }
    }

    private fun setViewStatus(lockStatus: Boolean) {
        databinding.llCollect.isEnabled = lockStatus
        databinding.etTitle.isEnabled = lockStatus
        databinding.etAuthor.isEnabled = lockStatus
        databinding.etLink.isEnabled = lockStatus
        if (lockStatus) {
            databinding.tvCollect.visibility = View.VISIBLE
            databinding.indicatorView.visibility = View.GONE
            databinding.indicatorView.hide()
        } else {
            databinding.tvCollect.visibility = View.GONE
            databinding.indicatorView.visibility = View.VISIBLE
            databinding.indicatorView.show()
        }
    }

}